package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.util.locking;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.ui.FormManager;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.PanelManager;

import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;

/**
 * @author Renato Miceli
 */
public class UnlockingFormPanel extends FormPanel implements FormManager<UserInfo> {

	private final String userLogin;

	private static String PASSWORD_FIELD = "pswd";

	public UnlockingFormPanel(PanelManager panelManager) {
		this.setBaseCls("x-plain");
		this.setLabelWidth(100);
		this.setWidth(250);
		this.setHeight(50);

		this.userLogin = panelManager.getInfoManager().getInfo().getLogin();

		Label component = new Label("Type password for user '" + userLogin + "'");
		this.add(component);

		TextField passwordField = new TextField("Password", PASSWORD_FIELD);
		passwordField.setInputType("password");
		passwordField.setMinLength(5);
		this.add(passwordField, new AnchorLayoutData("70%"));

		passwordField.focus();
	}

	/**
	 * {@inheritDoc}
	 */
	public UserInfo parseForm() {
		UserInfo userInfo = new UserInfo();
		Form form = this.getForm();
		userInfo.setLogin(userLogin);
		userInfo.setPassword(form.findField(PASSWORD_FIELD).getValueAsString());
		return userInfo;
	}
}
